﻿using EnvDTE80;

using System;
using System.Collections;
using System.Diagnostics;
using System.IO;

namespace Logging {
    public static class Logger
    {
#if DEBUG
        public static bool Enabled = true;
        private static int indent = 0;
        private static StreamWriter outFile = null;
#else
        public static bool Enabled = false;
#endif

        public static void SetOutputFile(FileStream stream)
        {
#if DEBUG
            if (outFile != null)
            {
                outFile.Close();
                outFile.Dispose();
            }
            outFile = new StreamWriter(stream);

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            Trace.AutoFlush = true;
#endif
        }
        public static void SetOutputFile(string filename)
        {
#if DEBUG
            SetOutputFile(new FileStream(filename, FileMode.Append));
#endif
        }
        public static void Close()
        {
#if DEBUG
            if (outFile != null)
            {
                outFile.Flush();
                outFile.Close();
            }
#endif
        }

        public static void Indent()
        {
#if DEBUG
            indent += 2;
#endif
        }
        public static void Deindent()
        {
#if DEBUG
            if (indent > 0)
            {
                indent -= 2;
            }
#endif
        }
        public static void Print(string str)
        {
#if DEBUG
            if (Enabled)
            {
                Trace.Write(str);
                if (outFile != null)
                {
                    outFile.Write(str);
                }
            }
            //outp.OutputString(str);
#endif
        }

        public static void PrintLine(string str)
        {
#if DEBUG
            for (int i = 0; i < indent; i++)
            {
                Print(" ");
            }
            Print(str + "\n");
#endif
        }
        public static void PrintLine()
        {
#if DEBUG
            PrintLine("");
#endif
        }

        public static void PrintHeader(string text)
        {
#if DEBUG
            if (text.Length > 0)
            {
                text = "- " + text + " -";
                if (text.Length % 2 == 0)
                {
                    text = "-" + text;
                }
            }
            while (text.Length < 78)
            {
                text = "-" + text + "-";
            }
            PrintLine(text);
#endif
        }
        public static void PrintSeparator()
        {
#if DEBUG
            PrintHeader("");
#endif
        }

        public static void PrintLineWithTime(string p)
        {
#if DEBUG
            PrintLine(DateTime.Now.ToString("o") + ": " + p);
#endif
        }
    }
}